package com.mall.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

public class DateUtil {

	public static String ymdhms = "yyyy-MM-dd HH:mm:ss";
	public static String ymd = "yyyy-MM-dd";

	public final static String CHINA_YMDHM = "yyyy年MM月dd日";
	public final static String CHINA_MD = "MM月dd日";
	public final static String CHINA_YMD = "yyyy年MM月dd日 HH时mm分ss秒";

	/**
	 * 将日期字符串转化为日期。失败返回null。
	 * 
	 * @param date
	 *            日期字符串
	 * @param parttern
	 *            日期格式
	 * @return 日期
	 */
	public static Date stringToDate(String date, String parttern) {
		Date myDate = null;
		if (date != null) {
			try {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(parttern);
				myDate = simpleDateFormat.parse(date);
			} catch (Exception e) {
			}
		}
		return myDate;
	}

	public static String dateToString(Date date, String parttern) {
		SimpleDateFormat sdf = new SimpleDateFormat(parttern);
		return sdf.format(date);
	}

	public static Date initDate(Date date) {
		Calendar currentDate = new GregorianCalendar();
		currentDate.setTime(date);
		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		return currentDate.getTime();
	}

	public static int compareDate(Date date1, Date date2) {
		Calendar c = Calendar.getInstance();
		c.setTime(date1);
		int year1 = c.get(Calendar.YEAR);
		int month1 = c.get(Calendar.MONTH);

		c.setTime(date2);
		int year2 = c.get(Calendar.YEAR);
		int month2 = c.get(Calendar.MONTH);

		int result;
		if (year1 == year2) {
			result = month1 - month2;
		} else {
			result = 12 * (year1 - year2) + month1 - month2;
		}
		return result;
	}

	/**
	 * @param date1
	 *            需要比较的时间 不能为空(null),需要正确的日期格式
	 * @param date2
	 *            被比较的时间 为空(null)则为当前时间
	 * @param stype
	 *            返回值类型 0为多少天，1为多少个月，2为多少年
	 * @return
	 */
	public static int compareDate(String date1, String date2, int stype) {
		int n = 0;

		String[] u = { "天", "月", "年" };
		String formatStyle = stype == 1 ? "yyyy-MM" : "yyyy-MM-dd";

		DateFormat df = new SimpleDateFormat(formatStyle);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(df.parse(date1));
			c2.setTime(df.parse(date2));
		} catch (Exception e3) {
			System.out.println("wrong occured");
		}
		// List list = new ArrayList();
		while (!c1.after(c2)) { // 循环对比，直到相等，n 就是所要的结果
			// list.add(df.format(c1.getTime())); // 这里可以把间隔的日期存到数组中 打印出来
			n++;
			if (stype == 1) {
				c1.add(Calendar.MONTH, 1); // 比较月份，月份+1
			} else {
				c1.add(Calendar.DATE, 1); // 比较天数，日期+1
			}
		}

		n = n - 1;

		if (stype == 2) {
			n = (int) n / 365;
		}

		System.out.println(date1 + " -- " + date2 + " 相差多少" + u[stype] + ":" + n);
		return n;
	}

	public static String dateToStringByCHINAYMDHM(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(CHINA_MD);
		return sdf.format(date);
	}
	
	public static String dateToStringByYmd(Date date) {
		if(date == null){
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(ymd);
		return sdf.format(date);
	}
	
	public static int compareDate(String date1, String date2) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(sdf.parse(date1));
		int year1 = c.get(Calendar.YEAR);
		int month1 = c.get(Calendar.MONTH);

		c.setTime(sdf.parse(date2));
		int year2 = c.get(Calendar.YEAR);
		int month2 = c.get(Calendar.MONTH);

		int result;
		if (year1 == year2) {
			result = month1 - month2;
		} else {
			result = 12 * (year1 - year2) + month1 - month2;
		}
		return result;
	}

	/**
	 * @param date1
	 *            需要比较的时间 不能为空(null),需要正确的日期格式
	 * @param date2
	 *            被比较的时间 为空(null)则为当前时间
	 * @param stype
	 *            返回值类型 0为多少天，1为多少个月，2为多少年
	 * @return
	 */
	public static int compareDate(Date date1, Date date2, int stype) {
		int n = 0;

		String[] u = { "天", "月", "年" };

		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(date1);
			c2.setTime(date2);
		} catch (Exception e3) {
			System.out.println("wrong occured");
		}
		// List list = new ArrayList();
		while (!c1.after(c2)) { // 循环对比，直到相等，n 就是所要的结果
			// list.add(df.format(c1.getTime())); // 这里可以把间隔的日期存到数组中 打印出来
			n++;
			if (stype == 1) {
				c1.add(Calendar.MONTH, 1); // 比较月份，月份+1
			} else {
				c1.add(Calendar.DATE, 1); // 比较天数，日期+1
			}
		}

		n = n - 1;

		if (stype == 2) {
			n = (int) n / 365;
		}

		System.out.println(date1 + " -- " + date2 + " 相差多少" + u[stype] + ":" + n);
		return n;
	}

	/**
	 * 返回日期加X天后的日期
	 * 
	 * @author dylan_xu
	 * @date Mar 11, 2012
	 * @param date
	 * @param i
	 * @return
	 */
	public static String addDay(Date date, int i) {
		return addDay(date, i, null);
	}

	/**
	 * 返回日期加X天后的日期
	 * 
	 * @author dylan_xu
	 * @date Mar 11, 2012
	 * @param date
	 * @param i
	 * @return
	 */
	public static String addDay(Date date, int i, String parttern) {

		if (StringUtils.isBlank(parttern)) {
			parttern = ymd;
		}
		try {
			SimpleDateFormat sdf_date_format = new SimpleDateFormat(parttern);
			Date addDayNo = DateUtils.addDays(date, i);
			return sdf_date_format.format(addDayNo);
		} catch (Exception e) {
			return getDate();
		}
	}

	/**
	 * 返回日期加X月后的日期
	 * 
	 * @author dylan_xu
	 * @date Mar 11, 2012
	 * @param date
	 * @param i
	 * @return
	 */
	public static String addMonth(Date date, int i) {
		try {
			SimpleDateFormat sdf_date_format = new SimpleDateFormat(ymd);
			Date addDayNo = DateUtils.addMonths(date, i);
			return sdf_date_format.format(addDayNo);
		} catch (Exception e) {
			return getDate();
		}
	}

	/**
	 * 获得服务器当前日期，以格式为：yyyy-MM-dd的日期字符串形式返回
	 * 
	 * @author dylan_xu
	 * @date Mar 11, 2012
	 * @return
	 */
	public static String getDate() {
		try {
			Calendar cale = Calendar.getInstance();
			SimpleDateFormat sdf_date_format = new SimpleDateFormat(ymd);
			return sdf_date_format.format(cale.getTime());
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 返回日期加X年后的日期
	 * 
	 * @author dylan_xu
	 * @date Mar 11, 2012
	 * @param date
	 * @param i
	 * @return
	 */
	public static String addYear(Date date, int i) {
		try {
			SimpleDateFormat sdf_date_format = new SimpleDateFormat(ymd);
			Date addDayNo = DateUtils.addYears(date, i);
			return sdf_date_format.format(addDayNo);
		} catch (Exception e) {
			return "";
		}
	}

	public static Date getJianGeMonth(Date date, int month) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, month);
		return cal.getTime();
	}

	public static void main(String[] args) {
		compareDate("2012-03-04", "2013-03-05", 2);
	}

	/**
	 * 获取当前时间，包括毫秒
	 * @return
	 */
	public static String getHaoMiaoShuAndDate() {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");// 其中yyyy-MM-dd是你要表示的格式
		// 可以任意组合，不限个数和次序；具体表示为：MM-month,dd-day,yyyy-year;kk-hour,mm-minute,ss-second;
		String str = sdf.format(d);
		return str;
	}
	
	/**
	 * 今天开始时间
	 * @return
	 */
	public static Date getTodayBeginTime(){
		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
		c.set(Calendar.HOUR_OF_DAY,0);
		c.set(Calendar.MINUTE,0);
		c.set(Calendar.SECOND,0);
		return c.getTime();
	}
	
	/**
	 * 今天结束时间
	 * @return
	 */
	public static Date getTodayEndTime(){
		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
		c.set(Calendar.HOUR_OF_DAY,23);
		c.set(Calendar.MINUTE,59);
		c.set(Calendar.SECOND,59);
		return c.getTime();
	}
}
